/********************************************************************************
 * SubCategoriesAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Oct 20, 2008 3:03:13 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.util.SearchCriteria;

public class SubCategoriesAction extends FrontBaseSupport implements ServletRequestAware {

	/** */
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private String subCategory;

	private List<GeneralCategories> listGeneralCategories = new ArrayList<GeneralCategories>();

	public SubCategoriesAction() {
		super(true, true);
		setCategoryDisplay(true);
	}

	public String execute() throws Exception {
		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute("generalCategories.categoryId", Integer.parseInt(this.request.getParameter("cid")));
		List<GeneralCategories> tmpGeneralCategories = (List<GeneralCategories>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, GeneralCategories.class);

		boolean flag = false;
		for (GeneralCategories generalCategories : tmpGeneralCategories) {

			if(flag) {
				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute("generalCategories.categoryId", generalCategories.getCategoryId());
				generalCategories.setGeneralCategoriesList((List<GeneralCategories>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, GeneralCategories.class));
				this.listGeneralCategories.add(generalCategories);
			} else {
				this.subCategory = generalCategories.getCategoryName();
				flag = true;
			}
		}

		return SUCCESS;
	}

//	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getSubCategory() {
		return subCategory;
	}

	public void setSubCategory(String subCategory) {
		this.subCategory = subCategory;
	}

	public List<GeneralCategories> getListGeneralCategories() {
		return listGeneralCategories;
	}

	public void setListGeneralCategories(
			List<GeneralCategories> listGeneralCategories) {
		this.listGeneralCategories = listGeneralCategories;
	}
}
